package com.shengyin.ec.controller.admin;

import java.awt.List;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shengyin.ec.controller.account.PersonalForm;
import com.shengyin.ec.model.entity.Company;
import com.shengyin.ec.model.entity.Personal;
import com.shengyin.ec.service.admin.*;

@Controller
public class AdminController {

	@Autowired
	AdminService adminService;
	
	@RequestMapping("/checkAdmin.do")
	public String checkAdmin(Model model, AdminForm adminForm, HttpServletRequest request){
		Company companyAdmin = new Company();
		try{
			
			BeanUtils.copyProperties(companyAdmin, adminForm);
			boolean flag = adminService.checkAdmin(adminForm.getUsername(), adminForm.getPwd());
			
			
			if(flag){
				HttpSession session = request.getSession();
				Company companyAdmin1 = adminService.getCompanyByAdminName(companyAdmin.getUsername());
				
				session.setAttribute("admin", companyAdmin1);
			
				return "redirect:/admin/index.jsp";
			}
			else{
				
				model.addAttribute("login", "error");
				return "redirect:/admin/login.jsp";
			}
		}catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return "redirect:/admin/login.jsp";
	}
	
	
}
